<?php
/**
 * 话题开奖
 */
namespace app\crontab\controller;
use app\common\model\community\CommunityCommentModel;
use app\common\model\community\CommunityCommentRankModel;
use app\common\model\community\CommunitySubjectModel;
use app\common\redis\AnalystActivityRankRedis;
use app\common\redis\UserCountingRedis;
use app\common\service\ElkLog;

class Community extends CrontabCommon {

	/**
	 * 话题开奖
	 */
	public function  lottery(){

		$subjectModel = CommunitySubjectModel::getInstance();
        $rankModel    = CommunityCommentRankModel::getInstance();
        $commentModel = CommunityCommentModel::getInstance();
        $time         = date('Y-m-d H:i:s', time());
        $map['is_lottery']   = -1;
        $map['lottery']      = ['neq', '[]'];
        $map['lottery_time'] = ['elt', $time];
        $ids    = $subjectModel->search_id_list_no_page($map, 'id', false, 'lottery_time');
        $lists  = $subjectModel->muti_get_data_by_id_list($ids);
        if ($lists) {
            foreach ($lists as &$list) {
                $comment_lists = $commentModel->get_comment_lists($list);
                foreach ($comment_lists as $k=>&$v) {
                    if (array_key_exists($k, $list['lottery']) && $v['rates'] >=1) {
                    	ElkLog::write( array( 'message'=> 'subject_lottery','user_id'=>$v['user_id'], 'subject_id'=>$list['id'], 'comment_id'=> $v['id'] ) );
                        $data = [
                            'subject_id' => $list['id'],
                            'user_id'    => $v['user_id'],
                            'comment_id' => $v['id'],
                            'rates'      => $v['rates'],
                            'rank'       => $k+1,
                            'lottery'     => $list['lottery'][$k],
                            'lottery_time'=> $list['lottery_time']
                        ];
                       	if ($list['is_lottery']==-1) {
                            $rankModel->add_data($data);
                        }
                        $subjectModel->update_data($list['id'], ['is_lottery'=>1]);
                    }
                }
                $list['comment_lists'] = $comment_lists;
            }
        }
	}

     /**
     * 获取话题（最后评论内容>=24字)
     */
    public function syn_last_post() {
        $subjectModel = CommunitySubjectModel::getInstance();
        $ids    =  $subjectModel->search_id_list_no_page(['status'=>1]);
        $lists  =  $subjectModel->muti_get_data_by_id_list($ids);
        foreach ($lists as &$v) {
            $subjectModel->syn_last_post($v['id']);
        }
        echo '同步完成';
    }

    public function format_up()
    {
        $UserCountingRedis = UserCountingRedis::getInstance();
        $UserCountingRedis->up_format();
    }

    public function compute_analyst()
    {
        $AnalystActivityRankRedis = AnalystActivityRankRedis::getInstance();
        $AnalystActivityRankRedis->compute_analyst();
    }

}